//
//  GroupNameViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/12.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "GroupNameViewController.h"
#import "BorderTextFieldView.h"

@interface GroupNameViewController ()
{
    EMGroup         *_group;
    BOOL            _isOwner;
    UITextField     *_subjectField;
}

@property(nonatomic,strong) BorderTextFieldView *nameTextFieldView;
@property(nonatomic,strong) UIButton *confirmButton;

@end

@implementation GroupNameViewController
- (instancetype)initWithGroup:(EMGroup *)group
{
    self = [self init];
    if (self) {
        _group = group;
        NSString *loginUsername = [[EMClient sharedClient] currentUsername];
        _isOwner = [_group.owner isEqualToString:loginUsername];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark --- 点击事件
- (void)didClickConfirmButton
{
    NSString *contentString = _nameTextFieldView.textField.text;
    if (contentString.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入群组名称"];
        return;
    }
    
    if (contentString.length > 12)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入小于12个字符的群组名称"];
        return;
    }
    
    EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:_group.groupId type:EMConversationTypeGroupChat createIfNotExist:NO];
    EMError *error = nil;
    [[EMClient sharedClient].groupManager changeGroupSubject:_nameTextFieldView.textField.text forGroup:_group.groupId error:&error];
    if (!error) {
        if ([_group.groupId isEqualToString:conversation.conversationId]) {
            NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:conversation.ext];
            [ext setObject:_group.subject forKey:@"subject"];
            [ext setObject:[NSNumber numberWithBool:_group.isPublic] forKey:@"isPublic"];
            conversation.ext = ext;
        }
        [self back];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"您没有修改群组名称的权限"];
    }
}

- (void)back
{
    if ([_subjectField isFirstResponder])
    {
        [_subjectField resignFirstResponder];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- 懒加载
- (UIButton *)confirmButton
{
    if(!_confirmButton)
    {
        UIView *superView = self.view;
        _confirmButton = [[UIButton alloc]init];
        [superView addSubview:_confirmButton];
        [_confirmButton setTitle:@"提交" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:fontCommonSize];
        [_confirmButton setBackgroundColor:rgba(0,159,236,1)];
        _confirmButton.layer.cornerRadius = 5;
        [_confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _confirmButton;
}

- (BorderTextFieldView *)nameTextFieldView
{
    if(!_nameTextFieldView)
    {
        UIView *superView = self.view;
        _nameTextFieldView = [[BorderTextFieldView alloc]init];
        [superView addSubview:_nameTextFieldView];
        _nameTextFieldView.textField.placeholder = @"请输入群组名称";
        [_nameTextFieldView.textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _nameTextFieldView;
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.nameTextFieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).with.offset(px1920Height(300));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(150));
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameTextFieldView.mas_bottom).with.offset(px1920Height(150));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"修改群组名称"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
